package boun.swe.bob.service;

import boun.swe.bob.entities.User;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.apache.commons.lang.xwork.StringEscapeUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * This controller is used for home page actions
 */
public class HomePageController extends BaseController {

    /**
     * Shows the page
     *
     * @param httpServletRequest  http request
     * @param httpServletResponse http response
     * @return page
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView showPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Map<String, Object> myModel = Maps.newHashMap();
        String feedCountStr = httpServletRequest.getParameter("feedCount");
        int feedCount = 5;
        if(null != feedCountStr) {
            feedCount = Integer.parseInt(feedCountStr) + 5;
        }
        myModel.put("himself", true);
        myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
        myModel.put("feedCount", feedCount);
        myModel.put("homefeeds", homeFeedDao.findHomeFeedsByUserId(getLoggedInUser(httpServletRequest).getUserId(), feedCount));
        return new ModelAndView("homePage", "model", myModel);

    }

    /**
     * Handles HTTP POST action
     *
     * @param httpServletRequest  http request
     * @param httpServletResponse http response
     * @return the result message
     */
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView handlePost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Map<String, Object> myModel = Maps.newHashMap();
        String feedCountStr = httpServletRequest.getParameter("feedCount");
        int feedCount = 5;
        if(null != feedCountStr) {
            feedCount = Integer.parseInt(feedCountStr) + 5;
        }
        String location = httpServletRequest.getParameter("location");
        String tweet = httpServletRequest.getParameter("tweet");
        try {
            location = StringEscapeUtils.unescapeHtml(location);
            tweet = StringEscapeUtils.unescapeHtml(tweet);
        }
        catch (Exception exp) {
        }


        User user = getLoggedInUser(httpServletRequest);

        if (!Strings.isNullOrEmpty(location)) {
            user.setLocation(location);
            userDao.update(user);
        }
        if (!Strings.isNullOrEmpty(tweet) && !tweet.equals("\r\n\r\n                ")) {
            compositeDao.addUserTweet(user, tweet);
        }

        myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
        myModel.put("feedCount", feedCount);
        myModel.put("homefeeds", homeFeedDao.findHomeFeedsByUserId(getLoggedInUser(httpServletRequest).getUserId(), feedCount));
        myModel.put("himself", true);

        return new ModelAndView("homePage", "model", myModel);

    }


}
